import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text("Function Widget Demo"),
      ),
      body: ShowDatePickerWidget(),
    ),
  ));
}

class ShowDatePickerWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      child: const Text("对话框"),
      onPressed: () async {
        await _showDatePicker2(context);
      },
    );
  }

  //  弹出对话框
  Future<DateTime?> _showDatePicker2(BuildContext context) async {
    var date = DateTime.now();
    return showCupertinoDialog(
      context: context,
      builder: (context) {
        return SizedBox(
          height: 200,
          child: CupertinoDatePicker(
            mode: CupertinoDatePickerMode.dateAndTime,
            minimumDate: date,
            maximumDate: date.add(const Duration(days: 30)),
            maximumYear: date.year + 1,
            onDateTimeChanged: (DateTime value) => print(value),
          ),
        );
      }
    );
  }
}
